using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UMC.Data;
using UMC.Web;
using UMC.WorkFlow.Entities;
using UMC.WorkFlow.Form;

namespace UMC.WorkFlow.Form
{


    class ControlActivity : WebActivity
    {
        public ControlActivity(TaskKey key)
        {
            this.TaskKey = key;
        }
        public TaskKey TaskKey { get; set; }

        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var key = this.AsyncDialog("Key", g => this.DialogValue("Promotion"));

            var Value = this.AsyncDialog("SettingValue", g =>
           {
               if (TaskKey.Type == 1)
               {
                   var ls = new List<String>(Utility.GetFaramKeys(this.TaskKey.SelectSQL));
                   ls.RemoveAll(k => String.Equals(k, "Keyword", StringComparison.CurrentCultureIgnoreCase));

                   foreach (var k in ls)
                   {
                       this.AsyncDialog(k, checd =>
                       {
                           this.Prompt(String.Format("缺少{0}参数", k));
                           return this.DialogValue("none");
                       });
                   }
                   var m = new WebMeta(request.Arguments);
                   m.Remove("Key");
                   var grid = new FormUIGridDialog(m.GetDictionary())
                   {
                       Title = TaskKey.Caption,
                       ProviderKey = this.TaskKey.ProviderKey,
                       SelectSQL = this.TaskKey.SelectSQL,
                       ValueKey = this.TaskKey.ValueKey
                   };
                   if (grid.Check() == false)
                   {
                       this.Prompt("脚本不正确");

                   }
                   grid.CloseEvent = "UI.Event";
                   return grid;
               }
               else
               {
                   var rds = new UIRadioDialog() { Title = this.TaskKey.Caption };

                   Utility.Each(DataFactory.Instance().TaskKeyValue(this.TaskKey.Id.Value).OrderBy(r => r.Seq ?? 0), dr =>
                   {
                       rds.Options.Add(dr.Text, dr.Value);
                   });
                   if (rds.Options.Count == 0)
                   {
                       this.Prompt("没配置子项");
                   }
                   if (rds.Options.Count > 5)
                   {

                       var grid = UIGridDialog.Create(new UIGridDialog.Header("Value", 0).PutField("Text", "名称").PutField("Value", "编码"), rds.Options.ToArray());
                       grid.Title = "组件类型";

                       return grid;
                   }
                   return rds;
               }
           });
            if (this.TaskKey.Type == 1)
            {

                var spIndex = Value.IndexOf('|');
                if (spIndex > -1)
                {
                    this.Context.UIEvent(key, new ListItem()
                    {
                        Value = Value.Substring(0, spIndex),
                        Text = Value.Substring(spIndex + 1)
                    }), true);
                }
                else
                {
                    this.Context.UIEvent(key, new ListItem()
                    {
                        Value = Value,
                        Text = Value
                    }), true);
                }
            }
            else
            {
                var KeyValue =   DataFactory.Instance().TaskKeyValue(this.TaskKey.Id.Value, Value);

                if (KeyValue == null)
                {
                    this.Prompt("无此子项");
                }

                this.Context.UIEvent(key, new ListItem()
                {
                    Value = KeyValue.Value,
                    Text = KeyValue.Text
                }), true);
            }

        }

    }

}